package com.huaguoguo.cyg.service.weibo.buss;

import com.huaguoguo.cyg.spider.pipeline.WeiboUserFilePipeline;
import com.huaguoguo.cyg.spider.pipeline.WeiboUserPipeline;
import com.huaguoguo.cyg.spider.processor.weibo.WeiboUserPageBarProcessor;
import com.huaguoguo.cyg.spider.processor.weibo.WeiboUserProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Spider;

@Service
public class WeiboUserService {
    @Autowired
    private WeiboUserPipeline weiboUserPipeline;

    @Autowired
    private WeiboUserFilePipeline weiboUserFilePipeline;

    /**
     * 爬取指定用户的微博home页和2次下拉菜单
     *
     * @param domain 域编号
     * @param uid    用户id
     * @param isHot  是否热门 1表示热门
     * @param page   页面数 从1开始
     */
    public void spiderUserHomeAndPagebar(String domain, String uid, Integer isHot, Integer page) {
        spiderUserHome(domain, uid, isHot, page);
        spiderUserHomePagebar(domain, uid, isHot, page, 0);
        spiderUserHomePagebar(domain, uid, isHot, page, 1);
    }

    /**
     * 爬取指定用户的微博home页和2次下拉菜单<br>
     *  读取到文件中
     * @param domain 域编号
     * @param uid    用户id
     * @param isHot  是否热门 1表示热门
     * @param page   页面数 从1开始
     */
    public void spiderUserHomeAndPagebarToJsonFile(String domain, String uid, Integer isHot, Integer page) {
        spiderUserHomeToJsonFIle(domain, uid, isHot, page);
        spiderUserHomePagebarToJsonFile(domain, uid, isHot, page, 0);
        spiderUserHomePagebarToJsonFile(domain, uid, isHot, page, 1);
    }

    /**
     * 爬取指定用户的微博home页
     *
     * @param domain 域编号
     * @param uid    用户id
     * @param isHot  是否热门 1表示热门
     * @param page   页面数 从1开始
     */
    public void spiderUserHome(String domain, String uid, Integer isHot, Integer page) {
        WeiboUserProcessor weiboUserProcessor = new WeiboUserProcessor();
        // 封装参数
        weiboUserProcessor.addData("domain", domain)
                .addData("uid", uid)
                .addData("isHot", isHot)
                .addData("page", page);
        // url ： https://weibo.com/p/{}{}/home?is_hot={}&page={}
        // explame ：100505 1876856920/home?is_hot=1&page=1
        Spider.create(weiboUserProcessor)
                .addUrl(weiboUserProcessor.buildRequestUrl())
                .addPipeline(weiboUserPipeline)
                .run();
    }

    /**
     * 爬取指定用户的微博home页<br>
     * 读取到文件中
     * @param domain 域编号
     * @param uid    用户id
     * @param isHot  是否热门 1表示热门
     * @param page   页面数 从1开始
     */
    public void spiderUserHomeToJsonFIle(String domain, String uid, Integer isHot, Integer page) {
        WeiboUserProcessor weiboUserProcessor = new WeiboUserProcessor();
        // 封装参数
        weiboUserProcessor.addData("domain", domain)
                .addData("uid", uid)
                .addData("isHot", isHot)
                .addData("page", page);
        // url ： https://weibo.com/p/{}{}/home?is_hot={}&page={}
        // explame ：100505 1876856920/home?is_hot=1&page=1
        Spider.create(weiboUserProcessor)
                .addUrl(weiboUserProcessor.buildRequestUrl())
                .addPipeline(weiboUserFilePipeline)
                .run();
    }


    /**
     * 爬取指定用户的微博home页的下拉加载
     *
     * @param domain
     * @param uid
     * @param pagebar 第几次加载， 从0开始
     * @param page
     */
    public void spiderUserHomePagebar(String domain, String uid, Integer isHot, Integer page, Integer pagebar) {
        WeiboUserPageBarProcessor weiboUserPageBarProcessor = new WeiboUserPageBarProcessor();
        // 封装参数
        weiboUserPageBarProcessor.addData("domain", domain)
                .addData("uid", uid)
                .addData("isHot", isHot)
                .addData("page", page)
                .addData("pagebar", pagebar);
        // url : http://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain={}&pagebar={}&id={}{}&page={}&pre_page={}&is_hot={}
        Spider.create(weiboUserPageBarProcessor)
                .addUrl(weiboUserPageBarProcessor.buildRequestUrl())
                .addPipeline(weiboUserPipeline)
                .run();
    }

    /**
     * 爬取指定用户的微博home页的下拉加载<br>
     * 读取到文件中
     * @param domain
     * @param uid
     * @param pagebar 第几次加载， 从0开始
     * @param page
     */
    public void spiderUserHomePagebarToJsonFile(String domain, String uid, Integer isHot, Integer page, Integer pagebar) {
        WeiboUserPageBarProcessor weiboUserPageBarProcessor = new WeiboUserPageBarProcessor();
        // 封装参数
        weiboUserPageBarProcessor.addData("domain", domain)
                .addData("uid", uid)
                .addData("isHot", isHot)
                .addData("page", page)
                .addData("pagebar", pagebar);
        // url : http://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain={}&pagebar={}&id={}{}&page={}&pre_page={}&is_hot={}
        Spider.create(weiboUserPageBarProcessor)
                .addUrl(weiboUserPageBarProcessor.buildRequestUrl())
                .addPipeline(weiboUserFilePipeline)
                .run();
    }

}
